package pub.guoxin.oops.common.auth;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import pub.guoxin.oops.enums.Authority;
import pub.guoxin.oops.model.User;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * Created by stephan on 20.03.16.
 */
@Data
public class JwtUserVo implements UserDetails  {

    private static final long serialVersionUID = -7492892930627297246L;
    @JsonIgnore
    private final String id;
    private final String username;
    private final String firstname;
    private final String lastname;
    @JsonIgnore
    private final String password;
    private final String email;
    private final String mobile;
    private final Collection<? extends GrantedAuthority> authorities;
    private final boolean enabled;
    @JsonIgnore
    private final Date lastPasswordResetDate;

    public JwtUserVo(String id, String username, String firstname, String lastname, String email, String mobile, String password,
                     Collection<? extends GrantedAuthority> authorities, boolean enabled, Date lastPasswordResetDate) {
        this.id = id;
        this.username = username;
        this.firstname = firstname;
        this.lastname = lastname;
        this.email = email;
        this.mobile = mobile;
        this.password = password;
        this.authorities = authorities;
        this.enabled = enabled;
        this.lastPasswordResetDate = lastPasswordResetDate;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    public User createUser(){
        return new User(null, password, username);
    }

    /**
     * User 转换 JwtUser
     * @return
     */
    public static JwtUserVo convert(User user) {
        return new JwtUserVo(
                user.getId(),
                user.getUsername(),
                user.getFirstname(),
                user.getLastname(),
                user.getEmail(),
                user.getMobile(),
                user.getPassword(),
                mapToGrantedAuthorities(user.getAuthorities()),
                user.getEnabled(),
                user.getLastPasswordResetDate()
        );
    }

    private static List<GrantedAuthority> mapToGrantedAuthorities(List<Authority> authorities) {
        List<GrantedAuthority> list = new ArrayList<>();
        for (Authority authority : authorities) {
            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(authority.name());
            list.add(simpleGrantedAuthority);
        }
        return list;
    }
}
